package com.ruoyi.legal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.legal.domain.vo.XstzBatchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.legal.mapper.XstzMapper;
import com.ruoyi.legal.domain.Xstz;
import com.ruoyi.legal.service.IXstzService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 线索台账Service业务层处理
 * 
 * @author xwj
 * @date 2025-04-22
 */
@Service
public class XstzServiceImpl extends ServiceImpl<XstzMapper, Xstz> implements IXstzService {
    @Autowired
    private XstzMapper xstzMapper;

    /**
     * 查询线索台账
     * 
     * @param id 线索台账主键
     * @return 线索台账
     */
    @Override
    public Xstz selectXstzById(Long id) {
        return xstzMapper.selectXstzById(id);
    }

    /**
     * 查询线索台账列表
     * 
     * @param xstz 线索台账
     * @return 线索台账
     */
    @Override
    public List<Xstz> selectXstzList(Xstz xstz) {
        return xstzMapper.selectXstzList(xstz);
    }

    /**
     * 新增线索台账
     * 
     * @param xstz 线索台账
     * @return 结果
     */
    @Override
    public Xstz insertXstz(Xstz xstz) {

      //  xstzMapper.insertXstz(xstz);
    save(xstz);
    return xstz;
    }

    /**
     * 修改线索台账
     * 
     * @param xstz 线索台账
     * @return 结果
     */
    @Override
    public Xstz updateXstz(Xstz xstz) {

      //  xstzMapper.updateXstz(xstz);
        updateById(xstz);
        return xstz;
    }

    /**
     * 批量删除线索台账
     * 
     * @param ids 需要删除的线索台账主键
     * @return 结果
     */
    @Override
    public int deleteXstzByIds(Long[] ids) {
        return xstzMapper.deleteXstzByIds(ids);
    }

    /**
    * 条件删除
    * @param xstz 线索台账
    * @return
    */
    @Override
    public int delete(Xstz xstz) {
        QueryWrapper<Xstz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, xstz);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param xstz 线索台账
     * @return
     */
    @Override
    public Pages<Xstz> queryPage(Xstz xstz) {
        QueryWrapper<Xstz> qw = new QueryWrapper<>();
        QueryTool.condition(qw, xstz);
        IPage<Xstz> ipage = new Query<Xstz>().getPage(xstz);

        IPage<Xstz> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    @Transactional
    public void batchSaveXstzBatch(XstzBatchVo xstzBatchVo, SysUser user) {
        List<Xstz> xstzList = xstzBatchVo.getList();
        List<Long> ids = xstzBatchVo.getDeleteIds();

        Date now = new Date();

        for(Xstz xstz: xstzList) {
            if(xstz.getId() == null) {
                xstz.setStatus("0");
                xstz.setCreateId(user.getUserId());
                xstz.setCreateBy(user.getNickName());
                xstz.setCreateTime(now);
            } else {
                xstz.setUpdateId(user.getUserId());
                xstz.setUpdateTime(DateUtils.getNowDate());
                xstz.setUpdateBy(user.getNickName());
            }
        }
        saveOrUpdateBatch(xstzList);
        if(ids != null && ids.size() > 0) {
            deleteXstzByIds(ids.toArray(new Long[0]));
        }

    }

    /**
     * 删除线索台账信息
     * 
     * @param id 线索台账主键
     * @return 结果
     */
    @Override
    public int deleteXstzById(Long id) {
        return xstzMapper.deleteXstzById(id);
    }
}
